/*
 * Copyright (c) 2008 SQLI http://www.sqli.com/
 * Author: Olivier CARPENTIER
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy 
 * of this software and associated documentation files (the "Software"), to deal in 
 * the Software without restriction, including without limitation the rights to use, 
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
 * Software, and to permit persons to whom the Software is furnished to do so, subject 
 * to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE 
 * USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace SQLI.WSS.WebControls.DataSource
{
    public partial class ConfigWSSListDataSourceOrderBy : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigWSSListDataSourceOrderBy"/> class.
        /// </summary>
        public ConfigWSSListDataSourceOrderBy()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the buttonOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            if ((string)this.comboBoxFieldSortBy.Text != string.Empty)
            {
                this.ComponentDataSource.SortField1 = (string)this.comboBoxFieldSortBy.Text;
                if (this.radioButtonSortIncrease.Checked)
                    this.ComponentDataSource.SortWayField1 = SortWay.Increase;
                else
                    this.ComponentDataSource.SortWayField1 = SortWay.Decrease;

                if ((string)this.comboBoxFieldAndBy.Text != string.Empty)
                {
                    this.ComponentDataSource.SortField2 = (string)this.comboBoxFieldAndBy.Text;
                    if (this.radioButtonAndIncrease.Checked)
                        this.ComponentDataSource.SortWayField2 = SortWay.Increase;
                    else
                        this.ComponentDataSource.SortWayField2 = SortWay.Decrease;
                }
            }

            this.Close();
        }

        /// <summary>
        /// Sets the fields.
        /// </summary>
        /// <param name="fields">The fields.</param>
        public void SetFields(List<string> fields)
        {
            this.comboBoxFieldAndBy.Items.Clear();
            this.comboBoxFieldSortBy.Items.Clear();
            foreach (string field in fields)
            {
                this.comboBoxFieldSortBy.Items.Add(field);
                this.comboBoxFieldAndBy.Items.Add(field);
            }

            if (ComponentDataSource.SortField1 != string.Empty && ComponentDataSource.SortField1 != null)
            {
                this.comboBoxFieldSortBy.Text = ComponentDataSource.SortField1;
                if (ComponentDataSource.SortWayField1 == SortWay.Increase)
                    this.radioButtonSortIncrease.Checked = true;
                else
                    this.radioButtonSortDecrease.Checked = true;

                if (ComponentDataSource.SortField2 != string.Empty &&
                    ComponentDataSource.SortField2 != null)
                {
                    this.comboBoxFieldAndBy.Text = ComponentDataSource.SortField2;
                    if (ComponentDataSource.SortWayField2 == SortWay.Increase)
                        this.radioButtonAndIncrease.Checked = true;
                    else
                        this.radioButtonAndDecrease.Checked = true;
                }
            }
        }

        private WSSListDataSource _component;
        /// <summary>
        /// Gets or sets the component data source.
        /// </summary>
        /// <value>The component data source.</value>
        public WSSListDataSource ComponentDataSource
        {
            get { return _component; }
            set { _component = value; }
        }
    }
}